home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware City / Control Strip / ControlStripShell ƒ / CSShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-20  |  5.8 KB  |  227 lines  |  [TEXT/MMCC]

  1. /* ---------------------------------------------------------------------------
  2. CSShell.c
  3.  
  4.     ©1994 Martin R. Wachter; All Rights Reserved.
  5.  
  6.     A sample Control Strip Module shell with a placeholder popup menu.
  7.     
  8.     NOTE: Requires the "ControlStrip.h" header available on 
  9.     the Developer CD series.
  10.     
  11. Version History
  12. 1.0 9/94    by Marty Wachter mrw@welchgate.welch.jhu.edu
  13.  
  14. Based on the MacCalendar Control Strip Sample by Martin Minow, MACDTS.
  15. --------------------------------------------------------------------------- */
  16.  
  17. #include "CSShell.h"
  18.  
  19. // ---------------------------------------------------------------------------
  20. //        • main
  21. // ---------------------------------------------------------------------------
  22. //
  23. // main entry point into our sdev
  24. //
  25. pascal long main (long message, long params, Rect *statusRect, GrafPtr statusPort)
  26. {
  27.     long result = 0L;
  28.     Str255    helpString;
  29.     
  30.     switch (message)
  31.     {
  32.         case sdevInitModule:         // check environs, allocate globals
  33.             result = DoCSInit();
  34.         break;
  35.         
  36.         case sdevCloseModule:         // release my memory
  37.             DoCSClose((MyGlobalHandle) params);
  38.         break;
  39.         
  40.         case sdevFeatures:             // let the strip track the mouse down
  41.             result = (  (1<<sdevWantMouseClicks)    // We handle mouse down
  42.                       | (1<<sdevDontAutoTrack)        // We track the mouse, too
  43.                       | (1<<sdevHasCustomHelp)        // Custom help string
  44.                     );
  45.             break;
  46.         break;
  47.         
  48.         case sdevGetDisplayWidth:    // inform the strip how much strip space we need
  49.             result = kIconWidth + width((*(*(MyGlobalHandle) params)->myArrowPict)->picFrame);
  50.         break;
  51.         
  52.         case sdevPeriodicTickle:    // no idle time used
  53.         break;
  54.         
  55.         case sdevDrawStatus:         // draw my icon and arrow pict
  56.             DoCSDraw((MyGlobalHandle) params, statusRect, statusPort);
  57.         break;
  58.         
  59.         case sdevMouseClick:         // the mouse was clicked & released in my button
  60.             DoCSClick((MyGlobalHandle) params, statusRect);
  61.         break;
  62.         
  63.         case sdevSaveSettings:        // no settings in this module
  64.         break;
  65.         
  66.         case sdevShowBalloonHelp:    // we have a custom ballon help string
  67.             SBGetDetachedIndString(helpString, (*(MyGlobalHandle) params)->myStrings, kHelpStringIndex);
  68.             SBShowHelpString(statusRect, helpString);
  69.         break;
  70.     }
  71.     return (result);
  72. }
  73.  
  74. // ---------------------------------------------------------------------------
  75. //        • DoCSClick
  76. // ---------------------------------------------------------------------------
  77. //
  78. // handle a click in our status rect
  79. //
  80. void DoCSClick(MyGlobalHandle myGlobals, const Rect    *statusRect)
  81. {
  82.     OSErr                iErr;
  83.     short                menuID;
  84.     MenuHandle            menuH;
  85.     
  86.     if ((*myGlobals)->myMenuH){
  87.         HLock((Handle)myGlobals);
  88.     
  89.         if ((*myGlobals)->myMenuH){
  90.             menuID = SBTrackPopupMenu(statusRect, (*myGlobals)->myMenuH);
  91.             
  92.             switch (menuID){
  93.                 
  94.                 case 1:
  95.                     break;
  96.                 case 2:
  97.                     break;
  98.                 default:
  99.                 
  100.                     break;
  101.             }                //end switch
  102.         }
  103.         HUnlock((Handle)myGlobals);
  104.     }
  105. }
  106.  
  107. // ---------------------------------------------------------------------------
  108. //        • DoCSInit
  109. // ---------------------------------------------------------------------------
  110. //
  111. // allocate memory for our globals and init our globals
  112. //
  113. long DoCSInit(void)
  114. {
  115.     MyGlobalHandle    myH;
  116.     OSErr            iErr;
  117.     Handle            iconSuite = 0L;
  118.     Str255            tempString;
  119.     
  120.     //
  121.     // get some memory
  122.     //
  123.     myH = (MyGlobalHandle) NewHandleClear(sizeof(MyGlobals));
  124.     
  125.     //
  126.     // got it
  127.     //
  128.     if (myH){    
  129.         HLock((Handle)myH);
  130.  
  131.     //
  132.     // get our icon suite
  133.     //
  134.         iErr = SBGetDetachIconSuite(&iconSuite, 669, 0x0000FF00L);
  135.         if (iErr) return iErr;
  136.         (*myH)->iconSuite = iconSuite;
  137.     //
  138.     // get our string list
  139.     //
  140.         (*myH)->myStrings = GetResource('STR#', 256);
  141.         iErr = ResError();
  142.         if (iErr) return iErr;
  143.         DetachResource((*myH)->myStrings);
  144.         iErr = ResError();
  145.         if (iErr) return iErr;
  146.     //
  147.     // get our popup menu
  148.     //
  149.         (*myH)->myMenuH = GetMenu(kPopupMenuID);
  150.         if (!(*myH)->myMenuH) return ResError();
  151.         DetachResource((Handle)(*myH)->myMenuH);
  152.         iErr = ResError();
  153.         if (iErr) return iErr;
  154.     //
  155.     // get our arrow picture
  156.     //
  157.         (*myH)->myArrowPict = GetPicture(kArrowPictID);
  158.         if (!(*myH)->myArrowPict) return ResError();
  159.         DetachResource((Handle)(*myH)->myArrowPict);
  160.         iErr = ResError();
  161.         if (iErr) return iErr;
  162.         HUnlock((Handle)myH);
  163.         return ((long) myH);
  164.     }
  165.     //
  166.     // did not get any memory, don't install
  167.     //
  168.     return (-1L);
  169. }
  170.  
  171. // ---------------------------------------------------------------------------
  172. //        • DoCSClose
  173. // ---------------------------------------------------------------------------
  174. //
  175. // free memory for our globals
  176. //
  177. void DoCSClose(MyGlobalHandle myGlobals)
  178. {
  179.     if (myGlobals){
  180.         HLock((Handle)myGlobals);
  181.         if ((*myGlobals)->iconSuite)
  182.             DisposeIconSuite ((*myGlobals)->iconSuite, true);
  183.         if ((*myGlobals)->myMenuH)
  184.             DisposeMenu((*myGlobals)->myMenuH);
  185.         if ((*myGlobals)->myStrings)
  186.             DisposeHandle((*myGlobals)->myStrings);
  187.         if ((*myGlobals)->myStrings)
  188.             KillPicture((*myGlobals)->myArrowPict);
  189.         DisposeHandle((Handle) myGlobals);
  190.     }
  191. }
  192.  
  193. // ---------------------------------------------------------------------------
  194. //        • DoCSDraw
  195. // ---------------------------------------------------------------------------
  196. //
  197. // draw our icon and arrow pict
  198. //
  199. void DoCSDraw(MyGlobalHandle myGlobals, Rect *statusRect, GrafPtr statusPort)
  200. {
  201.     OSErr        iErr;
  202.     Rect        viewRect;
  203.     short        alignment = 0x01 + 0x04; // center up & down , and left & right
  204.     long        transform = 0;              // no transform
  205.     short        arrowHeight = 0;
  206.     
  207.     if ((*myGlobals)->iconSuite){
  208.     //
  209.     // Draw our icon
  210.     //
  211.         viewRect = *statusRect;
  212.         viewRect.right = viewRect.left + kIconWidth;
  213.         (void)PlotIconSuite(&viewRect,atNone,ttNone,(*myGlobals)->iconSuite);
  214.     }
  215.     
  216.     //
  217.     // Draw our right-arrow pict to show that we have a popup menu
  218.     //
  219.     if ((*myGlobals)->myArrowPict){
  220.         arrowHeight = height((*(*myGlobals)->myArrowPict)->picFrame);
  221.         viewRect.left = viewRect.right;
  222.         viewRect.right += width((*(*myGlobals)->myArrowPict)->picFrame);
  223.         viewRect.top += ((height(viewRect) - arrowHeight) >> 1);
  224.         viewRect.bottom = viewRect.top + arrowHeight;
  225.         DrawPicture((*myGlobals)->myArrowPict, &viewRect);
  226.     }
  227. }